package com.novoda.runbuddy.model;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.Vector;

import android.location.Location;

public class Route {
	public final int					_id;
	public Vector<Location>				locations;
	public final int					sid;
	public final String					name;
	public final long					created_at;
	public Location						centerPoint	= new Location("location");
	private LinkedList<RouteSegment>	routeSegments;

	public Route(int route_id, int route_sid, String route_name, Vector<Location> locations, long route_created_at,
			long route_modified_at) {

		this._id = route_id;
		this.sid = route_sid;
		this.name = route_name;
		this.created_at = route_created_at;

		if (locations != null) {
			this.locations = locations;
		} else {
			locations = new Vector<Location>();
		}
	}

	public Route(int route_id, int route_sid, String route_name, LinkedList<RouteSegment> routeSegments,
			long route_created_at) {

		this._id = route_id;
		this.sid = route_sid;
		this.name = route_name;
		this.created_at = route_created_at;

		if (routeSegments != null) {
			this.routeSegments = routeSegments;
			this.locations = new Vector<Location>();
		} else {
			this.routeSegments = new LinkedList<RouteSegment>();
			this.locations = new Vector<Location>();
		}
	}

	public void addPoint(Location loc) {
		locations.add(loc);
	}

	public void addSegment(RouteSegment segment) {
		routeSegments.add(segment);
	}

	public LinkedList<RouteSegment> getRouteSegments() {
		return routeSegments;
	}

	// This function implemented for compatibility with existing code. May go
	// away later. - JJM
	public TreeMap<Long, Location> getlocationsAndTimes() {
		TreeMap<Long, Location> locationsAndTimes = new TreeMap<Long, Location>();

		Iterator<Location> points = locations.iterator();
		while (points.hasNext()) {
			Location point = points.next();
			locationsAndTimes.put(point.getTime(), point);
		}

		return locationsAndTimes;
	}
}
